<?php
/* sidebar
register_sidebar(array(
		'name' => 'sidebar-home',
		'id' => 'sidebar-home',
		'description' => 'Sidebar da Home',
		'before_widget' => '<div id="%1$s" class="%2$s">',
		'after_widget' => '</div>',
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	));
register_sidebar(array(
		'name' => 'sidebar-interna',
		'id' => 'sidebar-interna',
		'description' => 'Sidebar Interna',
		'before_widget' => '<div id="%1$s" class="%2$s">',
		'after_widget' => '</div>',
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
	));
	*/
add_theme_support('menus');
add_theme_support('post-thumbnails');
/*Tamanhos personalizados*/
add_image_size('produto', 158, 101, true);
add_image_size('destaque-home', 259, 142, true);
add_image_size('post-slideshow', 897, 375, true);
add_image_size('post-destaque-lista', 593, 131, true);
add_image_size('post-side-lista', 75, 71, true);
add_image_size('desenho-thumb', 213, 143, true);
add_image_size('jogo-thumb', 234, 217, true);
add_image_size('video-thumb', 112, 80, true);
add_image_size('area-bff', 516, 737, true);
add_image_size('wallpaper-thumb', 222, 146, true);
add_image_size('wallpaper-s', 1024, 768, true);
add_image_size('wallpaper-m', 1280, 800, true);
add_image_size('wallpaper-l', 1440, 900, true);
/*Tamanhos personalizados*/

function get_id_by_post_name($post_name)
{
	global $wpdb;
	$id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '".$post_name."'");
	return $id;
}

function searchfilter($query) {

    if ($query->is_search && !is_admin() ) {
        $query->set('post_type',array('post'));
    }

return $query;
}

function wpb_set_post_views($postID) {
    $count_key = 'wpb_post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
    }else{
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
}
//To keep the count accurate, lets get rid of prefetching
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

function wpb_track_post_views ($post_id) {
    if ( !is_single() ) return;
    if ( empty ( $post_id) ) {
        global $post;
        $post_id = $post->ID;    
    }
    wpb_set_post_views($post_id);
}
add_action( 'wp_head', 'wpb_track_post_views');

function wpb_get_post_views($postID){
    $count_key = 'wpb_post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        return "0 View";
    }
    return $count.' Views';
}

add_filter('pre_get_posts','searchfilter');

function login_with_email_address($username) {
        $user = get_user_by('email',$username);
        if(!empty($user->user_login))
                $username = $user->user_login;
        return $username;
}
add_action('wp_authenticate','login_with_email_address');

function change_username_wps_text($text){
       if(in_array($GLOBALS['pagenow'], array('wp-login.php'))){
         if ($text == 'Nome de utilizador'){$text = 'Email ou Login';}
            }
                return $text;
         }
add_filter( 'gettext', 'change_username_wps_text' );
   
?>